#include "LogWriter.h"

#include <wx/datetime.h>

#include "Util.h"
#include "MsnMessage.h"

LogWriter::LogWriter(const wxString& home, const wxString& chatPassport)
{
    wxFileName::Mkdir(home, 0777, wxPATH_MKDIR_FULL);

    wxString filePath = home + _T("/") + chatPassport + _T(".log");

    if(!logFile.Open(filePath, wxFile::write_append)){
        //todo warning
		DEBUG(_T("open log error"));
        return;
    }
}

LogWriter::~LogWriter()
{
    logFile.Close();
}


void LogWriter::Log(const wxString username, const wxString& content, bool newline)
{
    wxString time = wxDateTime::Now().Format(_T("%Y-%m-%d %H:%M:%S"));

    wxString buf;
	buf<<_T("[")<<time<<_T("]")<<username<<_T(":");

	if(newline){
		buf<<_T("\r\n");
	}

	buf<<content<<_T("\r\n");

    logFile.Write(buf);
    logFile.Flush();
}

void LogWriter::LogInstantMessage(InstantMessage* msg)
{
	Log(msg->GetContact(), msg->GetContent());
}
